package com.kun.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by likun on 2017/4/15.
 */
public class PropertiesUtil {
    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
    /**
     * 读取properties文件
     * @param fileName 配置文件路径
     * @param key 配置项名称
     * @return 读取成功返回配置文件的值，没找到key返回空字符串，加载文件失败返回null
     */
    public static String getProperties(String fileName,String key){
        Properties properties=new Properties();
        try {
            InputStream inputStream = new ClassPathResource(fileName).getInputStream();
            properties.load(inputStream);
            return properties.getProperty(key,"");//默认返回空字符串
        } catch (IOException e) {
            logger.error("读取配置文件"+fileName+"失败",e);
            e.printStackTrace();
        }
        return null;
    }
}
